package modules.majiang
{
	import com.netease.protobuf.UInt64;

	import communication.majiang.MajiangMsgEvent;

	import modules.ModuleManager;
	import modules.majiang.data.TablesDataManager;
	import modules.majiang.viewManager.MajiangUIManager;

	/**
	 *
	 * @author warden_feng
	 * 2013-5-7
	 */
	public class MajiangModuleManager extends ModuleManager
	{
		public function MajiangModuleManager()
		{
			init();
		}

		override protected function init():void
		{
			dispatcher.addEventListener(MSProto.MSID_ENTER_TABLE_ACK.toString(), onEnterTableAck);

			dispatcher.addEventListener(MSProto.MSID_LEAVE_TABLE_ACK.toString(), onLeaveTableAck);
		}

		/**
		 * 进入麻将桌子响应
		 */
		private function onEnterTableAck(event:MajiangMsgEvent):void
		{
			showMajiangUI(event.data.tableId);

			TablesDataManager.createTable(event.data);
		}

		/**
		 * 退出桌子
		 */
		public function onLeaveTableAck(event:MajiangMsgEvent):void
		{
			if (TablesDataManager.tableDataModeDic[event.data.tableIdStr] == null)
				return;
			var tableId:UInt64 = TablesDataManager.tableDataModeDic[event.data.tableIdStr].tableDataFromLobby.tableID;

			var gameUI:MajiangUIManager = UIAllRefer.instance.getGameUIbyTableId(tableId);
			gameUI.close();

			TablesDataManager.leaveTable(event.data.tableIdStr);
		}

		/**
		 * 显示大厅界面
		 */
		private function showMajiangUI(tableId:UInt64):void
		{
			logger("显示麻将游戏界面");

			var gameUI:MajiangUIManager = UIAllRefer.instance.getGameUIbyTableId(tableId);
			gameUI.show();

		}
	}
}
